package org.jeecg.modules.demo.mes.service.impl;

import org.apache.commons.lang3.StringUtils;
import org.jeecg.common.exception.JeecgBootException;
import org.jeecg.modules.demo.mes.entity.MesTechnologyProcedure;
import org.jeecg.modules.demo.mes.entity.MesTechnologyProcedureCheckBom;
import org.jeecg.modules.demo.mes.entity.MesTechnologyProcedureMaterialBom;
import org.jeecg.modules.demo.mes.mapper.MesTechnologyProcedureMapper;
import org.jeecg.modules.demo.mes.service.IMesTechnologyProcedureCheckBomService;
import org.jeecg.modules.demo.mes.service.IMesTechnologyProcedureMaterialBomService;
import org.jeecg.modules.demo.mes.service.IMesTechnologyProcedureService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description: 工艺工序表
 * @Author: jeecg-boot
 * @Date: 2022-10-31
 * @Version: V1.0
 */
@Service
public class MesTechnologyProcedureServiceImpl extends ServiceImpl<MesTechnologyProcedureMapper, MesTechnologyProcedure> implements IMesTechnologyProcedureService {
    @Autowired
    private IMesTechnologyProcedureCheckBomService mesTechnologyProcedureCheckBomService;
    @Autowired
    private IMesTechnologyProcedureMaterialBomService mesTechnologyProcedureMaterialBomService;
    /**
     * 通用工序表单表查询
     */
    @Override
    public List<MesTechnologyProcedure> queryTechnologyProcedureListSelective(Map<String, Object> cxMap) throws JeecgBootException {
        if (cxMap == null) {
            return new ArrayList<>();
        }
        return baseMapper.queryTechnologyProcedureListSelective(cxMap);
    }

    /**
     * 移除工序相关
     *
     * @param mesTechnologyProcedureId 工序id
     * @throws JeecgBootException
     */
    @Override
    @Transactional(readOnly = false, isolation = Isolation.DEFAULT, propagation = Propagation.REQUIRED)
    public void removeTechnologyProcedureById(String mesTechnologyProcedureId) throws JeecgBootException {
        //删除工序表
        this.removeById(mesTechnologyProcedureId);
        //删除BOM表
        final List<MesTechnologyProcedureCheckBom> mesTechnologyProcedureCheckBomList = mesTechnologyProcedureCheckBomService.queryTechnologyProcedureCheckBomListSelective(new HashMap<String, Object>() {{
            put("procedureId", mesTechnologyProcedureId);
        }});
        final List<MesTechnologyProcedureMaterialBom> mesTechnologyProcedureMaterialBomList = mesTechnologyProcedureMaterialBomService.queryTechnologyProcedureMaterialBomListSelective(new HashMap<String, Object>() {{
            put("procedureId", mesTechnologyProcedureId);
        }});
        if (mesTechnologyProcedureCheckBomList != null) {
            //删除检查BOM
            for (MesTechnologyProcedureCheckBom mesTechnologyProcedureCheckBom : mesTechnologyProcedureCheckBomList) {
                mesTechnologyProcedureCheckBomService.removeById(mesTechnologyProcedureCheckBom.getId());
            }
        }
        if (mesTechnologyProcedureMaterialBomList != null) {

            //删除物料BOM
            for (MesTechnologyProcedureMaterialBom mesTechnologyProcedureMaterialBom : mesTechnologyProcedureMaterialBomList) {
                mesTechnologyProcedureMaterialBomService.removeProcedureMaterialBomById(mesTechnologyProcedureMaterialBom.getId());
            }
        }
    }

    @Override
    public MesTechnologyProcedure queryTechnologyProcedureMaxByTechnologyId(String technologyId) throws JeecgBootException {
        if (StringUtils.isBlank(technologyId)){
            throw new JeecgBootException("technologyId is blank");
        }
        return this.baseMapper.queryTechnologyProcedureMaxByTechnologyId(technologyId);
    }
}
